package com.xiaobawang.xjsyy_idea_plugin.panel.config.view;

import com.intellij.openapi.ui.DialogWrapper;
import com.intellij.openapi.ui.Messages;
import com.xiaobawang.xjsyy_idea_plugin.global.GlobalConfig;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;

/**
 * @author synhard
 * @description 设置代码链接的对话框
 * @date 2024/5/23 11:06
 * @email songxinhang2023@e-chinalife.com
 * @id 18101119
 * @phone 13001321080
 */
public class GitRepoDialog extends DialogWrapper {
    private JTextField myGitRepoField;

    // 添加一个构造函数参数来接收TokenPanel的回调监听器
    private final GitRepoPanel.OnGitRepoUpdateListener gitRepoUpdateListener;

    public GitRepoDialog(GitRepoPanel.OnGitRepoUpdateListener listener) {
        super(true); // use current window as parent
        this.gitRepoUpdateListener = listener;
        setTitle("配置代码仓库");
        init();
    }

    @Nullable
    @Override
    protected JComponent createCenterPanel() {
        JPanel myPanel = new JPanel(new BorderLayout());
        myGitRepoField = new JTextField();
        myPanel.add(new JLabel("请输入代码仓库链接: "), BorderLayout.NORTH);
        myPanel.add(myGitRepoField, BorderLayout.CENTER);
        return myPanel;
    }

    @Override
    protected void doOKAction() {
        String gitRepo = myGitRepoField.getText();
        if (!gitRepo.isEmpty()) {
            // 代码仓库有效根据代码仓库选择对应的 app 给 AppSelectPanel
            GlobalConfig.gitRepo = gitRepo;
            if (gitRepoUpdateListener != null) {
                gitRepoUpdateListener.onGitRepoUpdated();
            }
            super.doOKAction();
        } else {
            // 代码仓库 无效
            Messages.showErrorDialog("输入的代码仓库不合法，请重新输入。", "错误");
        }
    }
}
